<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%	
String roleId = request.getParameter("roleId");
request.setAttribute("roleId",roleId); 
%>
<!DOCTYPE html">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
	body,html{
		margin: 0 auto;
		height:100%;
		width:100%;
	}
</style>
</head>
<body>
<div class="divfrist" id="system_roleAdd" style="width:100%;height:100%;overflow: auto;">
			<DIV id="" style="width:100%;height:0.7%;overflow:hidden;"></DIV>
			<DIV id="" style="width:100%;height:98.6%;">
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:50%;height:100%;">
							<DIV id="" class="allborder" style="width:100%;height:100%;max-height:100%;max-width:100%;overflow:auto;">
							   <!--内容区 -->									
										<div style="padding:10px 0 10px 60px">
										    <form id="system_roleAddff" method="post">
										        <input type="hidden" name="roleId">
										    	<table>
										    		<tr>
										    			<td>角色名:</td>
										    			<td><input  type="text" name="roleName" id="roleName"></input></td>
										    		</tr>
										    		<tr>
										    			<td>角色描述:</td>
										    			<td><input  type="text" name="roleNote" id="roleNote"></input></td>
										    		</tr>
										    		<tr>
										    			<td>角色状态:</td>
										    			<td>
										    			<select class="easyui-combobox" name="roleState" id="roleState">
										    				<option value="1">启用</option>
										    				<option value="0">禁用</option>
										    			</select>
										    			</td>
										    		</tr>										    		
										    	</table>
										    </form>
										    </div>
										    <div style="text-align:center;padding:5px">
										    	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="window.roleAdd.addRole()">确认</a>
										    	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="window.roleAdd.clearForm()">清空</a>
										    </div>										
							   <!--内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.4%;height:100%;overflow:hidden;"></DIV>
					<DIV id="" class="" style="float:left;width:48.8%;height:100%;margin-left:-1px;">
							<DIV id="" class="allborder" style="width:100%;height:100%;max-height:100%;max-width:100%;" >
								<!-- 内容区 -->
										 <ul id="addRole_tree" class="ztree"></ul>					
								<!-- 内容区 -->
							</DIV>
					</DIV>
					<DIV id="" class="" style="float:left;width:0.37%;height:100%;overflow:hidden;"></DIV>
			</DIV>
			<DIV id="" class="div_bottom"></DIV>
</div>
</body>
<script type="text/javascript">
(function(){
	
	//增加角色
	function addRole(){
		 var treeObj = $.fn.zTree.getZTreeObj("addRole_tree");
		 var nodes = treeObj.getCheckedNodes(true);
		 var sysPowerArray = new Array();
		 for(var i=0;i<nodes.length;i++){
			 var sysPower = new Object();
			 sysPower.sysPower_id=nodes[i].uuid;
			 sysPowerArray.push(sysPower);
		 }
		if(sysPowerArray.length<=0){
			 	alert("未选择权限");
				return;
		}
		var allPower = obj2str(sysPowerArray);
		$.messager.progress();
		$('#system_roleAddff').form('submit', {
				url: "${pageContext.request.contextPath}/sys/sysRoleAction!roleAdd.action?allPower="+allPower,
				onSubmit: function(){
				var isValid = $(this).form('validate');
				if (!isValid){
					$.messager.progress('close');	// 当form不合法的时候隐藏工具条
				}
				return isValid;	// 返回false将停止form提交 
			},
			success: function(data){
				$.messager.progress('close');
				if(data=="success"){
					alert("增加角色成功");
				}else if(data=="same"){
					alert("已经有相同名称的角色存在");
				}else if(data=="us"){
					alert("更新成功");
				}else{
					alert("操作失败");
				}
				// 当成功提交之后隐藏进度条
			}
		});
	}
	//创建树
	function createTree(treeDate){
		var setting = {
				data: {
					simpleData: {
						enable: true
					}
				},
				check: {
					enable: true
				}
		};
		if(treeDate==null||treeDate.length==0){
			$.ajax({
				   type: "POST",
				   url: "${pageContext.request.contextPath}/sys/sysPowerAction!getAllPowerZtree.action",
				   data:{roleId:0},
				   dataType:'json',
				   success: function(msg){
					   $.fn.zTree.init($("#addRole_tree"), setting, msg);     
				   }
			});	
		}else{
			 $.fn.zTree.init($("#addRole_tree"), setting, treeDate);    
		}
	}
	//清空
	function clearForm(){
		
	}
	//注册所有事件
	window["roleAdd"]={};
	window["roleAdd"]["addRole"]=addRole;
	window["roleAdd"]["clearForm"]=clearForm;
	//加载初始化
	$(function(){	
		$.parser.parse('#system_roleAdd'); 
		var valuetype=["length[2,30]","unnormal"];
		$('#roleNote').validatebox({   
		    required: true,   
		    validType: valuetype 
		});
		$('#roleName').validatebox({   
		    required: true,   
		    validType: valuetype 
		});  
		if("${roleId}"!=null&&"${roleId}"!=""){
			//加载修改数据
			$.ajax({
			   type: "POST",
			   async:false,
			   url: "${pageContext.request.contextPath}/sys/sysRoleAction!getRole.action",
			   data:{roleId:"${roleId}"},
			   dataType:'text',
			   success: function(msg){		
				    
				   var temp = eval('('+msg+')');				 
				   $('#system_roleAddff').form('load',temp.s);
				   createTree(temp.p);
			   },
			   error:function(a,b,c){
				   alert("查询角色失败");
				}
			});
		}else{
			//创建树
			createTree();	
		}
	});	
	
})();
</script>
</html>